
package com.example.elysiumease.mapper;

import com.example.elysiumease.model.Soul;
import com.example.elysiumease.model.StatusRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface SoulMapper {
    int insert(Soul soul);
    int update(Soul soul);
    int deleteById(Integer soulId);
    Soul selectById(Integer soulId);

    @Select("SELECT " +
            "s.current_status AS status, " +
            "MAX(l.created_at) AS changeTime, " +
            "u.angel_name AS operator, " +
            "l.log_details->>'$.new_status' AS details " +
            "FROM system_logs l " +
            "JOIN users u ON l.user_id = u.user_id " +
            "JOIN operation_types ot ON l.operation_type = ot.type_id " +
            "JOIN souls s ON l.target_id = s.soul_id " +
            "WHERE ot.type_name = 'STATUS_UPDATE' " +
            "AND l.target_id = #{soulId} " +
            "GROUP BY s.current_status " +
            "ORDER BY changeTime DESC")
    List<StatusRecord> selectStatusHistory(Integer soulId);

    //状态统计方法
    List<Map<String, Object>> countByStatus();
}